package com.homestay.controller;

import com.google.gson.Gson;
import com.homestay.beans.BookInfo;
import com.homestay.beans.Books;
import com.homestay.beans.Home;
import com.homestay.beans.Vip;
import com.homestay.service.BooksService;
import com.homestay.service.HomeService;
import com.homestay.service.VipService;
import com.homestay.service.impl.BooksServiceImpl;
import com.homestay.service.impl.HomeServiceImpl;
import com.homestay.service.impl.VipServiceImpl;
import com.homestay.utils.FormBeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/BooksServlet.do")
public class BooksServlet extends HttpServlet {

    VipService vipService = new VipServiceImpl();
    HomeService homeService = new HomeServiceImpl();
    BooksService booksService = new BooksServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String action = req.getParameter("action");
        switch (action) {
            case "bookHome":
                BookHome(req, resp);
                break;
            case "add":
                addBooks(req, resp);
                break;
            case "delete":
                deleteBooks(req, resp);
                break;
            case "go_update":
                goUpdate(req, resp);
                break;
            case "update":
                updateBooks(req, resp);
                break;
            case "list":
                GetBooks(req, resp);
                break;
            default:
                resp.getWriter().write("<script>alert('未知操作');location.href='views/index.jsp';</script>");
        }
    }

    private void goUpdate(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String id = req.getParameter("id");
            if (id == null) {
                id = "";
            }
            BookInfo bookInfo = booksService.getBookInfoById(id);
            if(bookInfo!=null){
                req.setAttribute("bookInfo", bookInfo);
            }
            req.getRequestDispatcher("views/guests_update.jsp").forward(req, resp);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void GetBooks(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String phone = req.getParameter("phone");
            if (phone == null) {
                phone = "";
            } else {
                phone = phone.trim();
            }
            List<BookInfo> bookInfoList = booksService.getBookInfo(phone);
            String json = new Gson().toJson(bookInfoList);
            resp.getWriter().write(json);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void BookHome(HttpServletRequest req, HttpServletResponse resp) {
        try {
            List<Vip> vipList = vipService.queryVipList("");
            List<Home> homeList = homeService.getHomesByState("空房");
            String home_id = req.getParameter("getHomeId");
            if(home_id!=null){
                Home home = homeService.getHomeById(Integer.parseInt(req.getParameter("getHomeId")));
                homeList.add(home);
            }

            Map<String, Object> map = new HashMap<>();
            map.put("vipList", vipList);
            map.put("homeList", homeList);
            String json = new Gson().toJson(map);
            resp.getWriter().write(json);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void addBooks(HttpServletRequest req, HttpServletResponse resp) {
        try {
            Books books = FormBeanUtils.populate(req, Books.class);
            int count = booksService.addBooks(books);
            if (count > 0) {
                resp.getWriter().write("true");
            } else {
                resp.getWriter().write("false");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void deleteBooks(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String id = req.getParameter("id");
            int count = booksService.deleteBooks(Integer.parseInt(id));
            if (count > 0) {
                resp.getWriter().write("true");
            } else {
                resp.getWriter().write("false");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void updateBooks(HttpServletRequest req, HttpServletResponse resp) {
        try {
            Books book = FormBeanUtils.populate(req, Books.class);
            String lastHomeId = req.getParameter("lastHomeId");
            int count = booksService.updateBooks(book, Integer.parseInt(lastHomeId));
            if(count > 0) {
                resp.getWriter().write("true");
            } else {
                resp.getWriter().write("false");
            }
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
